package com.zhupf.doublePoint;

/**
 * @author zhupf
 * @date 2023年10月25日 9:19
 * @Description
 */
public class MoveZeroes {

    public static void main(String[] args) {
        int[] nums = {0};
        moveZeroes(nums);
        for (int num : nums) {
            System.out.println(num);
        }
    }

    public static void moveZeroes(int[] nums) {
        int zeroeNum = 0;
        int idx = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == 0){
                if(zeroeNum == 0){
                    idx = i;
                }
                zeroeNum ++;
            }else {
                if(zeroeNum != 0){
                    nums[idx] = nums[i];
                    nums[i] = 0;
                    idx = zeroeNum == 1 ? i : idx +1;
                }
            }
        }
    }

    /**
     * 最优
     * @param nums
     */
    public static void moveZeroes3(int[] nums) {
        int idx = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] != 0){
                nums[idx++] = nums[i];
            }
        }
        for (int i = idx; i < nums.length; i++) {
           nums[i] = 0;
        }
    }


    /**
     * 官方 双指针
     * @param nums
     */
    public static void moveZeroes2(int[] nums) {
        int idx_left = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] != 0){
                int temp = nums[i];
                nums[i] = nums[idx_left];
                nums[idx_left] = temp;
                idx_left ++;
            }
        }
    }
}
